package com.lebang.medicalInquiry.controller;


import com.lebang.medicalInquiry.component.OssComponent;
import com.lebang.medicalInquiry.constant.ResponseEnum;
import com.lebang.medicalInquiry.response.Callback;
import com.lebang.medicalInquiry.response.JsonData;
import com.lebang.medicalInquiry.utils.TextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author ：ycx
 * @version :1.0
 * @date ：Created in 2021/9/2 14:57
 * @description：公用接口
 * @modified By：
 */
@RequestMapping("/common")
@Api(tags = "公用接口")
@RestController
public class CommonController {

    @Autowired
    OssComponent ossComponent;


    @ApiOperation("文件上传")
    @RequestMapping(value = "/file/upload", method = {RequestMethod.POST})
    @ResponseBody
    public JsonData headImgUpload(HttpServletRequest request,
                                  @ApiParam(name = "file", value = "图片上传", required = true)
                                  @RequestParam(name = "file", required = true)
                                          MultipartFile file,
                                  @ApiParam(name = "folder", value = "存储目录", required = true)
                                  @RequestParam(name = "folder", required = true)
                                          String folder,
                                  @ApiParam(name = "fileName", value = "文件名称", required = true)
                                  @RequestParam(name = "fileName", required = false)
                                          String fileName) {
        try {

            if (file == null || file.getSize() <= 0) {
                return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());
            }
            String s = folder.replaceAll("\r\n\r\n", "");
            String dataUrl = ossComponent.upload2Oss(file, s, fileName);
            if (!TextUtil.isEmpty(dataUrl)) {
                return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(),dataUrl);
            }
            return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());
    }





}